package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.Drinfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.RegMapper;
import com.ruoyi.system.domain.Reg;
import com.ruoyi.system.service.IRegService;

/**
 * 属性变量管理Service业务层处理
 * 
 * @author yaozh
 * @date 2024-03-01
 */
@Service
public class RegServiceImpl implements IRegService 
{
    private static final Logger log = LoggerFactory.getLogger(RegServiceImpl.class);
    @Autowired
    private RegMapper regMapper;

    @Override
    public List<Reg> findAllReg(String dBname) {
        return regMapper.findAllReg(dBname);
    }

    @Override
    public List<Reg> findAllByIsEnergy(String dbName, String isenergy) {
        List<Reg> regList = regMapper.findAllByIsEnergy(dbName, isenergy);//返回变量列表
        return regList;
    }

    @Override
    public int updateIsThreeDShowByRegIdList(String dbName, List<Long> subRegIdList, Integer isShow) {
        int rows = 0;
        try {
            rows = regMapper.updateIsThreeDShowByRegIdList(dbName, subRegIdList,isShow);

        } catch (Exception e) {
            e.printStackTrace();
            return rows;
        }
        return rows;
    }

    @Override
    public int updateTagNameByRegId(String dbName, Reg reg){
        int rows = 0;
        try {
            rows = regMapper.updateTagNameByRegId(dbName, reg);//更新变量
        } catch (Exception e) {
            e.printStackTrace();
            //throw new ServiceException("更新失败");
        }
        return rows;
    }

    /**
     * 查询属性变量管理
     * 
     * @param regId 属性变量管理主键
     * @return 属性变量管理
     */
    @Override
    public Reg selectRegByRegId(Long regId, String dbName)
    {
        return regMapper.selectRegByRegId(regId,dbName);
    }

    /**
     * 查询属性变量管理列表
     * 
     * @param reg 属性变量管理
     * @return 属性变量管理
     */
    @Override
    public List<Reg> selectRegList(Reg reg, String dbName)
    {
        return regMapper.selectRegList(reg,dbName);
    }

    @Override
    public List<Reg> selectRegListByDrIds(Reg reg, Long[] drIds, String dbName) {
        return regMapper.selectRegListByDrIds(reg,drIds,dbName);
    }

    /**
     * 新增属性变量管理
     * 
     * @param reg 属性变量管理
     * @return 结果
     */
    @Override
    public int insertReg(Reg reg, String dbName)
    {
        return regMapper.insertReg(reg,dbName);
    }

    /**
     * 修改属性变量管理
     * 
     * @param reg 属性变量管理
     * @return 结果
     */
    @Override
    public int updateReg(Reg reg, String dbName)
    {
        return regMapper.updateReg(reg,dbName);
    }

    /**
     * 批量删除属性变量管理
     * 
     * @param regIds 需要删除的属性变量管理主键
     * @return 结果
     */
    @Override
    public int deleteRegByRegIds(Long[] regIds, String dbName)
    {
        return regMapper.deleteRegByRegIds(regIds,dbName);
    }

    /**
     * 删除属性变量管理信息
     * 
     * @param regId 属性变量管理主键
     * @return 结果
     */
    @Override
    public int deleteRegByRegId(Long regId, String dbName)
    {
        return regMapper.deleteRegByRegId(regId,dbName);
    }

    @Override
    public int updateRegAlarmByTagName(String tagName, String isAlarm, String alarmLevelStr, String dbName){
        return regMapper.updateRegAlarmByTagName(tagName, isAlarm, alarmLevelStr, dbName);
    }

    @Override
    public String importReg(List<Reg> regList, Boolean isUpdateSupport, String operName, String dbName){
        if (com.ruoyi.common.utils.StringUtils.isNull(regList) || regList.size() == 0)
        {
            throw new ServiceException("导入项目设备属性展示数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Reg reg : regList)
        {
            try{
                // 验证是否存在，drId + regName 联合唯一主键（业务逻辑上）
                Long drId = reg.getDrId();
                String regName = reg.getRegName();
                Reg tempReg = regMapper.selectRegByDrIdAndRegName(drId,regName,dbName);
                if (tempReg == null)
                {
                    reg.setCreateBy(operName);
                    regMapper.insertReg(reg,dbName);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、设备ID " + reg.getDrId() + "、设备属性名称 " + reg.getRegName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    reg.setRegId(tempReg.getRegId());
                    reg.setUpdateBy(operName);
                    regMapper.updateReg(reg,dbName);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、设备ID " + reg.getDrId() + "、设备属性名称 " + reg.getRegName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、设备ID " + reg.getDrId() + "、设备属性名称 " + reg.getRegName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、设备ID " + reg.getDrId() + "、设备属性名称 " + reg.getRegName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }

        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
